<template>
  <div class="allsearch">
    <section class="search-content">
      <el-row :gutter="4" type="flex" justify="start">
        <el-col :span="24">
          <el-date-picker
            v-model="value"
            :picker-options="picker"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="|"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            style="width:300px;"
            @change="handleFilter"
          />
          <el-select v-model="option1" style="width:120px;" @change="handleFilter">
            <el-option
              v-for="item in options1"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
          <el-select v-model="option2" style="width:120px;" @change="handleFilter">
            <el-option
              v-for="item in options2"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
          <el-input
            v-model="keyword"
            placeholder="关键字"
            style="width:260px"
            @keyup.enter.native="handleFilter"
          />
          <el-button type="primary" @click="handleFilter">搜索</el-button>
          <el-button size="medium" type="info" @click="clearParams">清空</el-button>
          <el-button style="background-color:#e9ab3e;color:#fff;" @click="handleExport">导出</el-button>
        </el-col>
      </el-row>
    </section>
    <ul class="checknum">
      <li>
        网站净收入 :
        <span>{{ netIncome }}</span> 个
      </li>
      <li>
        收入 :
        <span>{{ income }}</span> 个
      </li>
      <li>
        支出 :
        <span>{{ outcome }}</span> 个
      </li>
    </ul>
    <subtable
      :table-label="tableLabel"
      :table-data="tableData.slice((currentpage-1)*pagesize,currentpage*pagesize)"
    >
      <el-table-column slot="first" label="流水号" align="center" min-width="20">
        <template slot-scope="scope">
          <span style="font-size:14px; color:#000;">{{ scope.row.sn }}</span>
        </template>
      </el-table-column>
      <el-table-column slot="second" label="名称" align="center" min-width="30">
        <template slot-scope="scope">
          <span style="font-size:14px; color:#000;">{{ scope.row.name }}</span>
          <br>
          <span style="font-size:12px;color:#8a8a8a">订单号: {{ scope.row.orderSn === undefined ? '----' : scope.row.orderSn }}</span>
          <br>
          <span style="font-size:12px;color:#8a8a8a">交易号: {{ scope.row.tradeSn === undefined ? '----' : scope.row.tradeSn }}</span>
        </template>
      </el-table-column>
      <el-table-column slot="third" label="用户名" align="center" min-width="10">
        <template slot-scope="scope">
          <el-button type="text" @click="handleClickDialog(scope)">{{ scope.row.userName }}</el-button>
        </template>
      </el-table-column>
      <el-table-column slot="isPro" label="网校收入" align="center" min-width="5">
        <template slot-scope="scope">
          <span v-if="scope.row.type === 'inflow'">+</span>
          <span v-if="scope.row.type === 'outflow'">-</span>
          <span>{{ scope.row.amount }}</span>
        </template>
      </el-table-column>
      <el-table-column slot="isEnd" label="支付方式" align="center" min-width="10">
        <template slot-scope="scope">
          <span>{{ scope.row.payment }}</span>
        </template>
      </el-table-column>
      <el-table-column slot="button" label="支付平台流水号" align="center" min-width="10">
        <template slot-scope="scope">
          <span>{{ scope.row.platform_sn === undefined ? '----' : scope.row.platform_sn }}</span>
        </template>
      </el-table-column>
    </subtable>
    <pagination
      :total="total"
      :current-page="currentpage"
      :page-size="pagesize"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />
    <bill-dialog
      :visiabledialog="billDialog"
      :dialog-data="billData"
      @changeVisiable="billDialog = false"
    />
  </div>
</template>

<script>
import BillDialog from '@/components/Dialog'
import Subtable from '@/components/Eltable'
import Pagination from '@/components/Pagination' // 分页组件
import { getCashFlowList } from '@/api/deal/cashFlow' // 数据接口
import { getUserDetail } from '@/api/deal/userDetail' // 数据接口
import { getTenantId } from '@/utils/auth'
export default {
  components: {
    Subtable,
    Pagination,
    BillDialog
  },
  data() {
    return {
      billDialog: false,
      billData: {},
      total: 0,
      currentpage: 1,
      pagesize: 10,
      netIncome: '0',
      income: '0',
      outcome: '0',
      tableLabel: [
        {
          label: '成交时间', width: '15', prop: 'createTimeStr'
        }
      ],
      tableData: [
        {
          username: '中科教育'
        }
      ],
      listFilter: [],
      value: [],
      option1: '',
      option2: '',
      keyword: '',
      options1: [
        {
          label: '支付方式',
          value: ''
        },
        {
          label: '支付宝',
          value: '支付宝'
        },
        {
          label: '微信',
          value: '微信'
        },
        {
          label: '虚拟币',
          value: '虚拟币'
        }
      ],
      options2: [
        {
          label: '选择类型',
          value: ''
        },
        {
          label: '用户号',
          value: '用户号'
        },
        {
          label: '流水号',
          value: '流水号'
        },
        {
          label: '订单号',
          value: '订单号'
        },
        {
          label: '名称',
          value: '名称'
        },
        {
          label: '交易号',
          value: '交易号'
        },
        {
          label: '支付平台流水号',
          value: '支付平台流水号'
        }
      ],
      picker: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  created() {
    this.getData()
  },
  updated() {
    this.statusfil()
  },
  methods: {
    statusfil() {
      let income = 0
      let outcome = 0
      this.tableData.map(item => {
        if (item.type === 'inflow') income += item.amount
        if (item.type === 'outflow') outcome += item.amount
      })
      this.netIncome = income - outcome
      this.income = income
      this.outcome = outcome
    },
    handleClickDialog(scope) {
      this.billDialog = true
      getUserDetail(scope.row.userId).then(response => {
        this.billData = response.data.item
      })
    },
    handleSizeChange(val) {
      console.log(val)
      this.currentpage = 1
      this.pagesize = val.value
      if (this.pagesize >= 20) {
        scrollTo(50, 800)
      }
    },
    handleCurrentChange(val) {
      console.log(val)
      this.currentpage = val.value
      if (this.pagesize >= 20) {
        scrollTo(50, 800)
      }
    },
    handleFilter() {
      this.tableData = this.listFilter.filter(item => {
        if (this.value && (new Date(item.createTime) < this.value[0] || new Date(item.createTime) > this.value[1])) return false
        if (this.option1 && item.payment !== this.option1) return false
        if (this.option2 === '用户号' && item.userName.toLowerCase().indexOf(this.keyword.toLowerCase()) === -1) return false
        if (this.option2 === '流水号' && item.sn.toString().toLowerCase().indexOf(this.keyword.toLowerCase()) === -1) return false
        if (this.option2 === '订单号' && item.orderSn.toString().toLowerCase().indexOf(this.keyword.toLowerCase()) === -1) return false
        if (this.option2 === '名称' && item.name.toLowerCase().indexOf(this.keyword.toLowerCase()) === -1) return false
        if (this.option2 === '交易号' && item.tradeSn.toString().toLowerCase().indexOf(this.keyword.toLowerCase()) === -1) return false
        if (this.option2 === '支付平台流水号' && item.orderSn.toString().toLowerCase().indexOf(this.keyword.toLowerCase()) === -1) return false
        return true
      })
      this.total = this.tableData.length
    },
    clearParams() {
      this.value = []
      this.option1 = ''
      this.option2 = ''
      this.getData()
    },
    handleExport() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['流水号', '名称', '用户名', '网校收入', '支付方式', '支付平台流水号']
        const filterVal = ['sn', 'name', 'userName', 'amount', 'payment', 'platform_sn']
        const list = this.tableData
        const data = this.formatJson(filterVal, list)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: 'cahsBillList',
          autoWidth: true,
          bookType: 'xlsx'
        })
        this.downloadLoading = false
      })
    },
    getData() {
      this.listLoading = true
      getCashFlowList('RMB').then(response => {
        this.tableData = response.data.items.filter(item => {
          if (item.tenantId !== getTenantId()) return false
          return true
        })
        this.tableData.map(item => {
          if (item.createTime) item.createTimeStr = new Date((item.createTime)).toLocaleString().replace(/[\u4e00-\u9fa5]/g, '')
        })
        this.listFilter = this.tableData
        this.total = this.tableData.length
        this.listLoading = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.allsearch {
  margin-top: 0;
  padding: 0;
  .search-content {
    width: 100%;
  }
}
.checknum {
  list-style: none;
  padding: 0;
  padding-left: 11px;
  margin: 0;
  margin-bottom: 21px;
  margin-top: 14px;
  height: 44px;
  line-height: 44px;
  box-sizing: border-box;
  background-color: #f8f8f8;
  li {
    float: left;
    display: block;
    margin-right: 20px;
    color: #72726f;
    span {
      color: rgb(15, 202, 62);
      font-weight: 600;
    }
  }
}
</style>
